package com.shqcjd.sinopecdemomobile.ui.activity;

import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.shqcjd.sinopecdemomobile.R;
import com.shqcjd.sinopecdemomobile.api.ApiService;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxObserver;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.sinopecdemomobile.model.ResponseData;
import com.shqcjd.sinopecdemomobile.model.pojo.ExpertBean;
import com.shqcjd.sinopecdemomobile.ui.base.BaseActivity;
import com.shqcjd.sinopecdemomobile.ui.base.BasePresenter;
import com.shqcjd.sinopecdemomobile.util.ToastUtils;
import com.zzhoujay.richtext.RichText;
import com.zzhoujay.richtext.callback.OnImageClickListener;
import com.zzhoujay.richtext.callback.OnUrlClickListener;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * 专家详情
 */
public class ExpertDetailActivity extends BaseActivity {

    @BindView(R.id.iv_toolbar_back)
    ImageView ivToolbarBack;
    @BindView(R.id.tv_toolbar_center)
    TextView tvToolbarCenter;
    @BindView(R.id.fl_launch_remote)
    FrameLayout flLaunchRemote;
    @BindView(R.id.iv_img)
    ImageView iv_img;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_expert_type)
    TextView tv_expert_type;
    @BindView(R.id.tv_info)
    TextView tv_info;

    private int expertId;
    private Context mContext;
    private ExpertBean data;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_expert_detail;
    }

    @Override
    public void initView() {
        mContext = this;
        tvToolbarCenter.setText("专家信息");
        expertId = getIntent().getIntExtra("ExpertId", -1);

        //解析富文本设置缓存目录
        RichText.initCacheDir(this);
        RichText.debugMode = true;
    }

    @Override
    public void initData() {
        getData();
    }

    private void getData() {
        ApiService.getExper(expertId)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<ExpertBean>>() {

                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                        showWaitingDialog("获取数据中");
                    }

                    @Override
                    public void _onNext(ResponseData<ExpertBean> retData) {
                        if (retData != null && retData.retCode == 10000) {
                            data = retData.data;
                            setData(retData.data);
                        } else {
                            ToastUtils.showShort(ExpertDetailActivity.this, retData.retMsg);
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        hideWaitingDialog();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });

    }

    private void setData(ExpertBean data) {
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.ic_default_small)	//加载成功之前占位图
                .error(R.mipmap.ic_default_small)	//加载错误之后的错误图
                .centerCrop();//指定图片的缩放类型为centerCrop （等比例缩放图片，直到图片的宽高都
        Glide.with(mContext)
                .load(data.icon)
                .apply(options)
                .into(iv_img);

        tv_name.setText(data.name);
//        if (data.free) {//	状态 true空闲 false忙碌
//            tv_state.setText("空闲");
//            tv_state.setTextColor(ContextCompat.getColor(mContext, R.color.expert_idle_color));
//            Drawable drawableLeft = getResources().getDrawable(R.drawable.shape_idle);
//            tv_state.setCompoundDrawablesWithIntrinsicBounds(drawableLeft, null, null, null);
//
//        } else {
//            tv_state.setText("忙碌");
//            tv_state.setTextColor(ContextCompat.getColor(mContext, R.color.expert_busy_color));
//            Drawable drawableLeft = getResources().getDrawable(R.drawable.shape_busy);
//            tv_state.setCompoundDrawablesWithIntrinsicBounds(drawableLeft, null, null, null);
//        }
//        tv_info.setText(data.info);

        tv_expert_type.setText(data.postName);

        RichText.from(data.info)
                .urlClick(new OnUrlClickListener() {
                    @Override
                    public boolean urlClicked(String url) {
                        if (url.startsWith("code://")) {
                            return true;
                        }
                        return false;
                    }
                })
                .imageClick(new OnImageClickListener() {
                    @Override
                    public void imageClicked(List<String> imageUrls, int position) {
                        //点击图片
                    }
                })
                .into(tv_info);


    }

    @OnClick({R.id.iv_toolbar_back, R.id.fl_launch_remote})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_toolbar_back:
                finish();
                break;
            case R.id.fl_launch_remote://发起远程指导
//                Intent intent = new Intent(ExpertDetailActivity.this, WaitingAnswerActivity.class);
//                intent.putExtra("remoteId", expertId);
//                intent.putExtra("equipmentId", equipmentId);
//                intent.putExtra("taskId", taskId);
//                intent.putExtra("icon", data.icon);
//                startActivity(intent);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RichText.recycle();
    }
}
